package com.cfpl.admin_service.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cfpl.admin_service.DTO.PlantLocationDTO;
import com.cfpl.admin_service.VO.FertilizationDetails;
import com.cfpl.admin_service.VO.PlantLocationVO;
import com.cfpl.admin_service.VO.VegEnvMonitoring;
import com.cfpl.admin_service.service.PlantingLocationService;
import com.cfpl.project_common.enums.CodeEnum;
import com.cfpl.project_common.exception.BusException;
import com.cfpl.project_common.pojo.PlantingLocation;
import com.cfpl.project_common.result.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Auther: LYP
 * @Date: 2023/10/23 - 11:49
 * @Description: com.cfpl.admin_service.controller
 * @version: 1.0
 * ------------------------------------------------
 * 种植果蔬名称查、种植位置查、种植户ID查
 * 谁   在哪  种了什么   种了多少   种植开始日期     预计收货日期
 * ------------------------------------------------
 * 种植地点信息表 vmid grower_id  ->  管理员表 admin_company   admin_id -> 肥料使用记录表  admin_id
 */
@RestController
@RequestMapping("/plantingLocation")
@Api(tags = "种植地点相关接口")
@Slf4j
public class PlantLocationController {

    @Autowired
    private PlantingLocationService plantingLocationService;


    /**
     * 种植果蔬名称查、种植地点查、种植户ID查
     * 1.无条件查：谁   在哪  种了什么   种了多少   种植开始日期     预计收货日期
     * 2.当前查询的种植户ID下的所有种植地点详细信息
     * 3.当前查询的种植地点有哪些种植户种植了那些果蔬
     * 4.当前查询的果蔬信息在哪些地方有哪些种植户种植
     * @param currentPage 当前页
     * @param pageSize 每页显示条数
     * @param plantLocationDTO 分页查询条件
     */
    @ApiOperation("条件分页查询种植地点信息")
    @PostMapping("/pageQueryPlantLocation")
    public BaseResult<IPage<PlantLocationVO>> pageQueryPlantLocation(
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize,
//            @RequestBody
            PlantLocationDTO plantLocationDTO
    ){
        Page<PlantLocationVO> page = new Page<>(currentPage, pageSize);
        IPage<PlantLocationVO> plantLocation = plantingLocationService.pageQueryPlantLocation(page,plantLocationDTO);

        return BaseResult.ok(plantLocation);
    }

    /**
     * 根据种植地点ID查询当前种植地点的所有肥料记录
     */
    @ApiOperation("根据种植地点ID查询当前种植地点的所有肥料记录")
    @GetMapping("/queryFertilizerByPlantLocationID/{id}")
    public BaseResult<IPage<FertilizationDetails>> queryFertilizerByPlantLocationID(
            @RequestParam(required = false,defaultValue = "1") int currentPage,
            @RequestParam(required = false,defaultValue = "10") int pageSize,
            @PathVariable("id") Long plantLocationID){
        if (plantLocationID == null){
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
        Page<FertilizationDetails> objectPage = new Page<>(currentPage, pageSize);
        IPage<FertilizationDetails> page = plantingLocationService.queryFertilizerByPlantLocationID(objectPage,plantLocationID);
        return BaseResult.ok(page);
    }

    /**
     * 添加种植户种植地点信息
     * @param plantingLocation 地点信息
     */
    @ApiOperation("添加种植户种植地点信息")
    @PostMapping("/insertPlantLocationInfo")
    public BaseResult insertPlantLocationInfo(@RequestBody PlantingLocation plantingLocation){
        plantingLocationService.save(plantingLocation);
        return BaseResult.ok();
    }

    /**
     * 种植地点信息
     * @param plantingLocation 地点信息
     */
    @PostMapping("/updatePlantLocationInfo")
    @ApiOperation("更新种植地点信息")
    public BaseResult updatePlantLocationInfo(@RequestBody PlantingLocation plantingLocation){
        plantingLocationService.updateById(plantingLocation);
        return BaseResult.ok();
    }
    /**
     * 功能描述：根据种植地点id查询某一具体果蔬的环境监测数据"
     * @param id 种植地点id
     * @return
     */
    @ApiOperation("根据种植地点id查询某一具体果蔬的环境监测数据")
    @GetMapping("/queryVegByEnvMonitoring/{locationId}")
    public BaseResult<List<VegEnvMonitoring>> queryVegByEnvMonitoring(@PathVariable("locationId") Long id) {
        List<VegEnvMonitoring> list = plantingLocationService.queryVegByEnvMonitoring(id);
        return BaseResult.ok(list);
    }


}
